%%
%% Task 5
%%

declare
fun {ApplyRules Ms}
   case Ms of A|B|C then
      case A of trackA(N) then
	 case B of trackA(M) then
	    trackA(N+M)|C
	 else
	    A|{ApplyRules B|C}
	 end
      
      [] trackB(N) then	    
	 case B of trackB(M) then
	    trackB(N+M)|C
	 else
	    A|{ApplyRules B|C}	       
	 end	    
      end	 
   [] H|T then
      case H of trackA(N) then
	 case T of trackA(M) then
	    trackA(N+M)
	 else
	    H|T
	 end
      
      [] trackB(N) then	    
	 case T of trackB(M) then
	    trackB(N+M)
	 else
	    H|T	       
	 end	    
      end
   else
      {RemoveZero Ms}
   end
end


fun {RemoveZero Moves}
   case Moves of H|T then
      case H of trackA(0) then
	 {RemoveZero T}
      [] trackB(0) then
	 {RemoveZero T}
      else
	 H|{RemoveZero T}
      end
   else
      nil
   end
end